package com.lwj.liwanjia.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by 001 on 2017/7/14.
 */

public class BaseUtils {

    //将指定日期转化为秒数
    public static String getSecond(String expireDate) {

        if (expireDate == null || expireDate.trim().equals("")) {
            return null;
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date date = null;
        try {
            date = sdf.parse(expireDate);
            return String.valueOf((date.getTime() / 1000));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

    }

    //秒数转化为字符串
    public static String getString(String seconds) {
        if (seconds == null)
            return " ";
        else {
            Date date = new Date();
            try {
                date.setTime(Long.parseLong(seconds) * 1000);
            } catch (NumberFormatException nfe) {
                nfe.printStackTrace();
                return "Input string:" + seconds + "not correct,eg:2011-01-20";
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return sdf.format(date);
        }
    }

    //String类型转化为Date
    public static Date getDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        date = formatter.parse(strTime);
        return date;
    }

    //秒数转化为字符串
    public static String getStringNew(String seconds) {
        if (seconds == null)
            return " ";
        else {
            Date date = new Date();
            try {
                date.setTime(Long.parseLong(seconds) * 1000);
            } catch (NumberFormatException nfe) {
                nfe.printStackTrace();
                return "Input string:" + seconds + "not correct,eg:2011-01-20";
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(date);
        }
    }
}
